"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 目录无权限	双机场景，主机和备机data目录中其他目录无权限，双机重建；双机切换
Create At   : 2020/07/02
@zou_jialiang0505328114
Description :
    步骤 1.启动数据库
    步骤 2.修改主机data目录中base权限
    步骤 3.备机重建
    步骤 4.恢复主机data目录中base权限
    步骤 5.备机重建恢复主备状态
    步骤 6.修改备机data目录中base权限
    步骤 7.备机重建
    步骤 8.恢复备机data目录中base权限
    步骤 9.备机重建恢复主备状态
Expect      :
    步骤 1.启动数据库成功
    步骤 2.修改主机data目录中base权限成功
    步骤 3.备机重建成功
    步骤 4.恢复主机data目录中base权限成功
    步骤 5.备机重建恢复主备状态成功
    步骤 6.修改备机data目录中base权限成功
    步骤 7.备机重建成功
    步骤 8.恢复备机data目录中base权限成功
    步骤 9.备机重建恢复主备状态成功
History     :
    Modified by @wan005 2021/7/14: 调整wal_sender_timeout,规避因环境性能差导致的失败
    Modified by @wan005 2021/7/22: 根据开发(00572622)建议调整wal_sender_timeout
"""
import unittest
from yat.test import macro
from yat.test import Node
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *


class Deletaduit(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_standby = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case028.py start------------')
        self.rootNode = Node()
        self.primary_dbuser = Node(node='PrimaryDbUser')
        self.primary_root = Node(node='PrimaryRoot')
        self.standby1_root = Node(node='Standby1Root')
        self.standby1_dbuser = Node(node='Standby1DbUser')
        self.Constant = Constant()

        result = self.sh_primy.executDbSql('show wal_sender_timeout;')
        self.log.info(f"wal_sender_timeout is {result}")
        self.wal_sender_timeout = result.strip().splitlines()[-2]

    def test_startdb(self):
        self.log.info('----修改 wal_sender_timeout------')
        result = self.sh_primy.executeGsguc('reload',
                                            self.Constant.GSGUC_SUCCESS_MSG,
                                            'wal_sender_timeout=160s')
        self.assertTrue(result)


        # 修改主机data目录中base权限
        self.log.info('修改主机data目录中base权限')
        chmodcmd = 'chmod 000 ' + macro.DB_INSTANCE_PATH + '/base'
        self.primary_root.sh(chmodcmd)
        self.log.info(chmodcmd)

        # 备机重建
        self.log.info('备机重建')
        rebuild_msg = self.sh_standby.executeGsctl('build', self.Constant.PERMISSION_DENIED)
        self.log.info(rebuild_msg)
        self.assertTrue(rebuild_msg)

        # 恢复主机data目录中base权限
        self.log.info('恢复主机data目录中base权限')
        chmodcmd1 = 'chmod 700 ' + macro.DB_INSTANCE_PATH + '/base'
        self.primary_root.sh(chmodcmd1)
        self.log.info(chmodcmd1)

        # 备机重建恢复主备状态
        self.log.info('备机重建恢复主备状态')
        rebuild_msg = self.sh_standby.executeGsctl('build', self.Constant.REBUILD_SUCCESS_MSG)
        self.log.info(rebuild_msg)
        self.assertTrue(rebuild_msg)

        status = self.sh_primy.getDbClusterStatus('status')
        self.log.info(status)

        # 修改备机data目录中base权限
        self.log.info('修改备机data目录中base权限')
        self.standby1_root.sh(chmodcmd)
        self.log.info(chmodcmd)

        # 备机重建
        self.log.info('备机重建')
        build_result = self.Constant.FAILED_TO_REMOVE_DIR + f" {macro.DB_INSTANCE_PATH}" + "/base"
        rebuild_msg = self.sh_standby.buildStandby()
        self.log.info(rebuild_msg)
        self.assertTrue(self.Constant.PERMISSION_DENIED in rebuild_msg or build_result in rebuild_msg)

        # 恢复备机data目录中base权限
        self.log.info('恢复备机data目录中base权限')
        self.standby1_root.sh(chmodcmd1)
        self.log.info(chmodcmd1)

        # 备机重建恢复主备状态
        self.log.info('备机重建恢复主备状态')
        rebuild_msg = self.sh_standby.executeGsctl('build', self.Constant.REBUILD_SUCCESS_MSG, '-b full')
        self.log.info(rebuild_msg)
        self.assertTrue(rebuild_msg)

    def tearDown(self):
        db_status = self.sh_primy.getDbClusterStatus('status')
        if not db_status:
            chmodcmd1 = 'chmod 700 ' + macro.DB_INSTANCE_PATH + '/base'
            self.log.info(chmodcmd1)
            self.log.info('恢复主机data目录中base权限')
            self.primary_root.sh(chmodcmd1)

            self.log.info('恢复备机data目录中base权限')
            self.standby1_root.sh(chmodcmd1)

            self.log.info('备机重建恢复主备状态')
            rebuild_msg = self.sh_standby.executeGsctl('build', self.Constant.REBUILD_SUCCESS_MSG, '-b full')
            self.log.info(rebuild_msg)
            self.assertTrue(rebuild_msg)
        else:
            pass
        result = self.sh_primy.executeGsguc('reload',
                                          self.Constant.GSGUC_SUCCESS_MSG,
                                          f'wal_sender_timeout='
                                          f'{self.wal_sender_timeout}')
        self.log.info(result)